<template>
  <el-card class="me-area" :body-style="{ padding: '16px' }">
    <div class="me-article-header">

      <a @click="view(id)" class="me-article-title">{{title}}</a>
      <el-button v-if="weight > 0" class="me-article-icon" type="text">置顶</el-button>
      <span class="me-pull-right me-article-count">
	    	<i class="me-icon-comment"></i>&nbsp;{{commentCounts}}
	    </span>
      <span class="me-pull-right me-article-count">
	    	<i class="el-icon-view"></i>&nbsp;{{viewCounts}}
	    </span>
    </div>

    <div class="me-artile-description">
      {{summary}}
    </div>
    <div class="me-article-footer">
	  	<span class="me-article-author">
        <i class="me-icon-author"></i><span>作者：</span>&nbsp;{{author}}
	    </span>

      <el-tag v-for="t in tags" :key="t.tagName" size="mini" type="success">{{t.tagName}}</el-tag>

      <span class="me-pull-right me-article-count">
	    	<i class="el-icon-time"></i>&nbsp;{{createDate | format}}
	    </span>

    </div>
    <div style="padding: 20px" v-show="uid==this.userId">
      <el-button type="primary" class="me-pull-right" size="mini" @click="updateArticle(id)">修改</el-button>
      <el-button type="danger" class="me-pull-right" size="mini" style="margin-right: 10px" @click="deleteArticle(id)">
        删除
      </el-button>
    </div>
  </el-card>

</template>

<script>
  import {Message} from 'element-ui';
  import {formatTime} from "../../utils/time";
  import {deleteArticle} from '@/api/article'
  import {getUserInfo} from '@/api/login'
  import {getToken, getUserName} from '@/request/token'
  import store from '@/store'
  export default {
    inject: ['reload'],
    name: 'ArticleItem',
    props: {
      id: String,
      weight: Number,
      title: String,
      commentCounts: Number,
      viewCounts: Number,
      summary: String,
      author: String,
      uid: String,
      tags: Array,
      createDate: String
    },
    data() {
      return {
        token: localStorage.token,
        userId: '',
        isShow: false
      }
    },
    created() {
      this.checkAuthor()
    },
    methods: {
      checkAuthor() {
/*        if (typeof (this.token) == "undefined") {
        } else {
          getUserInfo(this.token).then(resp => {
             this.userId=resp.data.id
            console.log("userId="+resp.data.id)
          })
          console.log("store.state.id="+store.state.id)
        }*/
        this.userId=store.state.id
      },
      view(id) {
        this.$router.push({path: `/view/${id}`})
      }
      ,
      updateArticle(id) {

        if (typeof (getToken()) == "undefined") {
          Message({
            type: 'warning',
            showClose: true,
            message: '请先登录哦'
          })
        } else {
          this.$router.push({path: `/write/${id}`})
        }
      }
      ,
      deleteArticle(id) {
        let that = this
        if (typeof (getToken()) == "undefined") {
          Message({
            type: 'warning',
            showClose: true,
            message: '请先登录哦'
          })
        } else {
          if (confirm("是否确认要删除")) {
            deleteArticle(id).then(resp => {
              if (resp.success) {
                that.$message({message: '删除成功', type: 'success', showClose: true})
              } else {
                that.$message({message: error, type: 'error' + resp.msg, showClose: true});
              }
              this.reload()

            })
          } else {

          }
        }


      }
      ,
    }
  }
</script>

<style scoped>

  .me-article-header {
    /*padding: 10px 18px;*/
    padding-bottom: 10px;
  }

  .me-article-title {
    font-weight: 600;
  }

  .me-article-icon {
    padding: 3px 8px;
  }

  .me-article-count {
    color: #a6a6a6;
    padding-left: 14px;
    font-size: 13px;
  }

  .me-pull-right {
    float: right;
  }

  .me-artile-description {
    font-size: 13px;
    line-height: 24px;
    margin-bottom: 10px;
  }

  .me-article-author {
    color: #a6a6a6;
    padding-right: 18px;
    font-size: 13px;
  }

  .el-tag {
    margin-left: 6px;
  }

</style>
